package com.sie.config;

import com.github.xiaoymin.knife4j.spring.annotations.EnableKnife4j;
import com.google.common.collect.Lists;
import com.hustcad.plm.pdm.common.config.SwaggerConfig;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Primary;
import springfox.bean.validators.configuration.BeanValidatorPluginsConfiguration;
import springfox.documentation.annotations.ApiIgnore;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * @author linjinde
 */
@Primary
@Configuration
@EnableSwagger2
@EnableKnife4j
@Slf4j
@Import({BeanValidatorPluginsConfiguration.class})
@ConditionalOnProperty(
        name = {"swagger.enabled"},
        havingValue = "true"
)
public class MySwaggerConfig extends SwaggerConfig{

    private static final Class<?>[] IGNORED_PARAMETER_TYPES = new Class[]{ServletRequest.class, ServletResponse.class, HttpServletRequest.class, HttpServletResponse.class, HttpSession.class, ApiIgnore.class};
    @Bean({"2.我的项目接口"})
    public Docket docket3() {
        return (new Docket(DocumentationType.SWAGGER_2)).groupName("我的项目接口").ignoredParameterTypes(IGNORED_PARAMETER_TYPES).apiInfo(this.getApiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.sie.controller")).apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any()).build().securitySchemes(this.securitySchemes()).securitySchemes(Lists.newArrayList(new ApiKey[]{this.apiKey()})).securityContexts(Collections.singletonList(this.securityContext()));
    }
    @Bean({"1.中台项目接口"})
    public Docket docket2() {
        return (new Docket(DocumentationType.SWAGGER_2)).groupName("1.中台项目接口").ignoredParameterTypes(IGNORED_PARAMETER_TYPES).apiInfo(this.getApiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.hustcad.plm")).apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)).paths(PathSelectors.any()).build().securitySchemes(this.securitySchemes()).securitySchemes(Lists.newArrayList(new ApiKey[]{this.apiKey()})).securityContexts(Collections.singletonList(this.securityContext()));
    }

    @Bean({"0.底座接口"})
    public Docket docket1() {
        return (new Docket(DocumentationType.SWAGGER_2)).groupName("0.底座接口").ignoredParameterTypes(IGNORED_PARAMETER_TYPES).apiInfo(this.getApiInfo()).select().apis(RequestHandlerSelectors.basePackage("com.ty.backend")).paths(PathSelectors.any()).build().securitySchemes(this.securitySchemes()).securitySchemes(Lists.newArrayList(new ApiKey[]{this.apiKey()})).securityContexts(Collections.singletonList(this.securityContext()));
    }

    private ApiInfo getApiInfo() {
        return (new ApiInfoBuilder()).title("JavaPDM2022 API文档").description("JavaPDM2022 API文档项目接口").termsOfServiceUrl("http://www.hustcad.com/").version("1.0").contact(new Contact("天喻软件研发团队", "http://www.hustcad.com/", "info@hustcad.com")).build();
    }

    private List<? extends SecurityScheme> securitySchemes() {
        List<SecurityScheme> list = new ArrayList<>();
        list.add(new BasicAuth("Authorization"));
        return list;
    }

    private ApiKey apiKey() {
        return new ApiKey("Bearer", "Authorization", "header");
    }

    private SecurityContext securityContext() {
        return SecurityContext.builder().securityReferences(this.defaultAuth()).forPaths(PathSelectors.any()).build();
    }

    private List<SecurityReference> defaultAuth() {
        AuthorizationScope authorizationScope = new AuthorizationScope("global", "accessEverything");
        AuthorizationScope[] authorizationScopes = new AuthorizationScope[]{authorizationScope};
        return Arrays.asList(new SecurityReference("Bearer", authorizationScopes));
    }
}
